package com.example.rental_system.controller;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.rental_system.pojo.Admin;
import com.example.rental_system.pojo.House;
import com.example.rental_system.pojo.User;
import com.example.rental_system.service.HouseService;
import com.example.rental_system.service.UserService;
import com.example.rental_system.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/userCon")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;
    //用户登陆
    @RequestMapping("/userLogin")
    public String userLogin(User user, HttpSession session, Model model){

        System.out.println(user);
        //判断用户名密码是否正确
        QueryWrapper<User> qw=new QueryWrapper<>();
        qw.eq("user_name",user.getUserName());
        User one = userService.getOne(qw);
        if (one!=null && one.getPassword().equals(user.getPassword())){
            session.setAttribute("user",one);
            session.setAttribute("userName",one.getUserName());
            IPage<House> houseIPage=houseService.queryByUserId(0,1,3);
            int[] rainbow = PageUtil.rainbow(1, (int)houseIPage.getPages(), 3);
            model.addAttribute("pageInfo",houseIPage.getRecords());//页面信息
            model.addAttribute("pageNo",houseIPage.getCurrent());//当前页
            model.addAttribute("totalPages",houseIPage.getPages());//总页数
            model.addAttribute("rainbow",rainbow);
            return "userMain";
        }else {
            model.addAttribute("msg", ResultUtils.FAILURE);
            return "index";
        }

    }
    //用户登录首页
    @RequestMapping("/userFragment")
    public String userFragment(){
        return "userFragment";
    }
    //前往修改密码
    @RequestMapping("/toChangePassword")
    public String toChangePassword(){
        return "changeUserPassword";
    }
    //修改密码
    @RequestMapping("/changePassword")
    public String changePassword(String oldPassword,String newPassword,Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        //如果旧密码同session密码相同，执行修改
        if (oldPassword.equals(user.getPassword())){
            user.setPassword(newPassword);
            QueryWrapper qw=new QueryWrapper();
            qw.eq("user_name",user.getUserName());
            userService.update(user,qw);
            session.removeAttribute("user");
            session.removeAttribute("userName");
            return "redirect:/";
        }else {
            model.addAttribute("msg","原密码输入错误");
        }
        return "changeAdminPassword";
    }
    //前往我的房屋列表
    @RequestMapping("/myHouse")
    public String myHouse(Model model,HttpSession session){
        String userName = (String)session.getAttribute("userName");

        IPage<House> houseIPage=houseService.queryHouseByUserId(userName,1,3);
        int[] rainbow = PageUtil.rainbow(1, (int)houseIPage.getPages(), 3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "userRent";
    }
    //我的房屋列表分页
    @RequestMapping("/housePage/{pageNo}")
    public String housePage(@PathVariable Integer pageNo, Model model,HttpSession session){
        String userName = (String) session.getAttribute("userName");
        IPage<House> houseIPage=houseService.queryHouseByUserId(userName,pageNo,3);

        int[] rainbow =PageUtil.rainbow(pageNo,(int)houseIPage.getTotal(),3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "userRent";
    }
    //我的房屋详情
    @RequestMapping("/details/{id}")
    public String details(@PathVariable Integer id,Model model){
        QueryWrapper<House> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id);
        House one = houseService.getOne(queryWrapper);
        model.addAttribute("house",one);
        return "userDetails";
    }
    //我的房屋退租
    @RequestMapping("/exitRent/{id}")
    public String exitRent(@PathVariable Integer id){
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        House one = houseService.getOne(queryWrapper);
        one.setUserId("0");
        houseService.update(one,queryWrapper);
        return "userFragment";
    }
    //前往发布房屋
    @RequestMapping("/toSendHouse")
    public String toSendHouse(){
        return "userSendHouse";
    }
    //发布房屋
    @RequestMapping("/userSendHouse")
    public String userSendHouse(MultipartFile file,House house,HttpSession session){
        String url=houseService.upload(file);
        house.setUserId("0");
        house.setImg(url);
        house.setPublisher((String) session.getAttribute("userName"));
        System.out.println(house);
        houseService.save(house);
        return "userSendHouse";
    }
    //我发布的房屋列表
    @RequestMapping("/toUsersHouse")
    public String toUsersHouse(Model model,HttpSession session){
        String userName = (String)session.getAttribute("userName");
        IPage<House> houseIPage = houseService.queryHousesByUserId(userName, 1, 3);
        int[] rainbow =PageUtil.rainbow(1,(int)houseIPage.getTotal(),3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "userBeRentHouse";
    }

    //我发布的房屋列表分页s
    @RequestMapping("/housePage1/{pageNo}")
    public String housePage1(@PathVariable Integer pageNo, Model model,HttpSession session){
        String userName = (String) session.getAttribute("userName");
        IPage<House> houseIPage=houseService.queryHousesByUserId(userName,pageNo,3);

        int[] rainbow =PageUtil.rainbow(pageNo,(int)houseIPage.getTotal(),3);
        model.addAttribute("pageInfo",houseIPage.getRecords());
        model.addAttribute("pageNo",houseIPage.getCurrent());
        model.addAttribute("totalPages",houseIPage.getTotal());
        model.addAttribute("rainbow",rainbow);
        return "userBeRentHouse";
    }
    @RequestMapping("/toUserDelSend/{id}")
    public String toUserDelSend(@PathVariable Integer id,Model model){
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        House one = houseService.getOne(queryWrapper);
        model.addAttribute("house",one);
        return "userDelSend";
    }
    @RequestMapping("/delSend/{id}")
    public String delSend(@PathVariable Integer id){
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        House one = houseService.getOne(queryWrapper);
        houseService.remove(queryWrapper);
        return "userFragment";
    }

}
